<?php

namespace app\index\controller;

use app\index\model\NewsModel;
use app\index\model\SceneryModel;
use app\index\model\SearchModel;
use think\Db;
use think\Jwt;
use think\View;
use think\Request;

class Index extends Base {
    public function __construct(Request $request) {
        parent::__construct($request);
        View::share('menuNow', '/index.html');
    }

    public function index() {
        return view('index', [

        ]);
    }

    public function sidebar() {
        $sidebarList = [];
        //获取新闻列表
        $newsModel                          = new NewsModel();
        $scenery                            = new SceneryModel();
        $sidebarList['notice_list']         = $newsModel->getList(['n.type' => ['in', '8'], 'status' => '1'], 1, 1);
        $sidebarList['news_list']           = $newsModel->getList(['n.type' => ['in', '3,7'], 'status' => '1'], 1, 5);
        $sidebarList['scenery_list']        = $scenery->getList([], 1, 3);
        $sidebarList['search_list']['data'] = Db::query("SELECT
                COUNT(id) AS num,
                keywords
            FROM
                tps_search
            GROUP BY
                keywords
            ORDER BY
                num DESC
            LIMIT 10");
        return json($sidebarList);
    }

    public function getUserInfo() {
        $jwt      = Jwt::getInstance();
        $userInfo = $jwt->get('user_info');
        if (empty($userInfo)) {
            if (!empty($this->uid)) {
                $userInfo = Db::table('tps_user')->field('nick,user_type')->where(['id' => $this->uid])->find();
                $jwt->set('user_info', json_encode($userInfo));
            } else {
                $userInfo = [];
            }
        } else {
            $userInfo = json_decode($userInfo, true);
        }
        //获取用户信息
        return json($userInfo);
    }

    public function search(Request $request) {
        $keywords     = $request->get('keywords');
        $keywordsList = Db::query("SELECT
                COUNT(id) AS num,
                keywords
            FROM
                tps_search
            GROUP BY
                keywords
            ORDER BY
                num DESC
            LIMIT 30");
        if (empty($keywords)) {
            $list            = [];
            $list['news']    = [];
            $list['scenery'] = [];
            return view('search', [
                'keywordsList' => $keywordsList,
                'keywords' => "",
                'list'     => $list
            ]);
        } else {
            //获取景点
            $newsModel = new NewsModel();
            $newsList  = $newsModel->getSearch($keywords);
            //获取新闻
            $sceneryModel = new SceneryModel();
            $sceneryList  = $sceneryModel->getSearch($keywords);
            //数组合并
            $list            = [];
            $list['news']    = $newsList;
            $list['scenery'] = $sceneryList;
            //记录日志
            if (!empty($newsList) || !empty($sceneryList)) {
                $searchModel = new SearchModel();
                $searchModel->addSearch($keywords);
            }
            return view('search', [
                'keywordsList' => $keywordsList,
                'keywords'     => $keywords,
                'list'         => $list
            ]);
        }
    }
}
